import Opt from '@/components/dt/info/Opt.js'
import table from '@/components/dt/table/render.js'
import clueCustomerMgr from '@/biz/customer/clueCustomer.js'
import rolesOpt from '@/biz/option/roles.js'
import dateOpt from '@/biz/option/date.js'

let _this = null

class Column extends Opt {
  constructor(vm) {
    super()
    _this = vm
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    let company = [
      {
        field: 'divisionName',
        title: '事业部',
        minWidth: 160
      },
      {
        field: 'companyName',
        title: '分公司',
        minWidth: 160
      },
      {
        field: 'officeName',
        title: '事业处',
        minWidth: 160
      },
      {
        field: 'buildingName',
        title: '所属案场',
        minWidth: 160
      }
    ]
    let caseList = [
      {
        field: 'createCount',
        title: '创建数',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'createCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'saleOfficeCount',
        title: '线上售楼处展示数',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'saleOfficeCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'readCount',
        title: '线上售楼处阅读数',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'readCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'userCount',
        title: '顾问数',
        minWidth: 160
      },
      {
        field: 'quoteCount',
        title: '推广总次数',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'quoteCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'aveQuoteCount',
        title: '人均推广次数',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'aveQuoteCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'scanCount',
        title: '扫码总次数',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'scanCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'addClueCount',
        title: '新增线索总数',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'addClueCountSort',
          desc: '2',
          asc: '1'
        }
      }
    ]
    let userList = [
      {
        title: '推广人员',
        minWidth: 150,
        align: 'center',
        header: {
          filter: {
            title: '推广人员',
            key: 'expendUserId',
            placeholder: '请选择推广人员',
            load: (o) => {
              o.select.options = clueCustomerMgr.selectTeamUser('', false)
            },
            select: {
              options: [],
              multiple: false,
              filterable: true,
              remote: (key) => {
                return clueCustomerMgr.selectTeamUser(key, false)
              }
            }
          }
        },
        cell: {
          value: (row) => {
            return `${row.expendUser}`
          },
          text: {
            tooltip: false
          }
        }
      },
      {
        field: 'role',
        minWidth: 140,
        header: {
          filter: rolesOpt.create({
            key: 'roleId',
            title: '角色',
            multiple: false
          })
        }
      },
      {
        field: 'onScene',
        title: '是否在案',
        minWidth: 140,
        header: {
          filter: {
            title: '是否在案',
            key: 'onScene',
            placeholder: '请选择',
            select: {
              options: [
                { key: 1, value: '在案' },
                { key: 2, value: '离案' }
              ]
            }
          }
        }
      },
      {
        field: 'quoteCount',
        title: '推广总次数',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'quoteCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'dayQuoteCount',
        title: '日均推广次数',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'dayQuoteCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'scanCount',
        title: '扫码总次数',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'scanCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'addClueCount',
        title: '新增线索总数',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'addClueCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'wechatShareCount',
        title: '微信分享次数',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'wechatShareCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'scanShareCount',
        title: '扫码数（分享）',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'scanShareCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'addClueShareCount',
        title: '新增线索数（分享）',
        minWidth: 180,
        remoteSort: true,
        sort: {
          key: 'addClueShareCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'friendShareCount',
        title: '朋友圈分享次数',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'friendShareCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'scanFriendCount',
        title: '扫码数（朋友圈）',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'scanFriendCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'addClueFriendCount',
        title: '新增线索数（朋友圈）',
        minWidth: 200,
        remoteSort: true,
        sort: {
          key: 'addClueFriendCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'savePicShareCount',
        title: '保存图片数',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'savePicShareCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'scanPicShareCount',
        title: '扫码数（保存图片）',
        minWidth: 180,
        remoteSort: true,
        sort: {
          key: 'scanPicShareCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'addCluePicShareCount',
        title: '新增线索（保存图片）',
        minWidth: 200,
        remoteSort: true,
        sort: {
          key: 'addCluePicShareCountSort',
          desc: '2',
          asc: '1'
        }
      }
    ]
    let contentList = [
      {
        field: 'articleTitle',
        title: '霸屏神器名',
        minWidth: 160
      },

      {
        title: '霸屏神器预览',
        minWidth: 180,
        slots: {
          default: (params, h) => {
            let img = {
              event: params.row.previewUrl ? 'lookImg' : '',
              src: params.row.previewUrl ? params.row.previewUrl : '-',
              data: [params.row.previewUrl ? params.row.previewUrl : '-']
            }
            return table.cell.img(_this, h, img, true)
          }
        }
      },
      {
        field: 'scanCodeType',
        title: '扫码落地页',
        minWidth: 160,
        header: {
          filter: {
            key: 'scanCodeType',
            title: '扫码落地页',
            placeholder: '请选择扫码落地页',
            select: {
              key: 'scanCodeType'
            }
          }
        }
      },
      {
        field: 'saleOffice',
        title: '是否在线上售楼处展示',
        minWidth: 180,
        header: {
          filter: {
            key: 'saleOffice',
            title: '是否在线上售楼处展示',
            placeholder: '请选择扫码落地页',
            select: {
              options: [
                { key: 1, value: '是' },
                { key: 0, value: '否' }
              ]
            }
          }
        }
      },
      {
        field: 'readCount',
        title: '线上售楼处阅读数',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'readCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'quoteCount',
        title: '推广总次数',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'quoteCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'scanCount',
        title: '扫码总次数',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'scanCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'addClueCount',
        title: '新增线索总数',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'addClueCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'wechatShareCount',
        title: '微信分享次数',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'wechatShareCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'scanShareCount',
        title: '扫码数（分享）',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'scanShareCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'addClueShareCount',
        title: '新增线索数（分享）',
        minWidth: 180,
        remoteSort: true,
        sort: {
          key: 'addClueShareCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'friendShareCount',
        title: '朋友圈分享次数',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'friendShareCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'scanFriendCount',
        title: '扫码数（朋友圈）',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'scanFriendCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'addClueFriendCount',
        title: '新增线索数（朋友圈）',
        minWidth: 200,
        remoteSort: true,
        sort: {
          key: 'addClueFriendCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'savePicShareCount',
        title: '保存图片数',
        minWidth: 160,
        remoteSort: true,
        sort: {
          key: 'savePicShareCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'scanPicShareCount',
        title: '扫码数（保存图片）',
        minWidth: 180,
        remoteSort: true,
        sort: {
          key: 'scanPicShareCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        field: 'addCluePicShareCount',
        title: '新增线索（保存图片）',
        minWidth: 200,
        remoteSort: true,
        sort: {
          key: 'addCluePicShareCountSort',
          desc: '2',
          asc: '1'
        }
      },
      {
        title: '创建时间',
        field: 'createDate',
        minWidth: 180,
        header: {
          filter: dateOpt.range({
            title: '创建时间',
            placeholder: '请选择创建时间',
            startKey: 'crStartDate',
            endKey: 'crEndDate',
            disabled: (date) => {
              return date.valueOf() > new Date().getTime()
            },
            check: (val) => {
              if (_this.$tool.dateDiff(val.crStartDate, val.crEndDate).Y >= 2) {
                // 日期最大跨度两年
                _this.$Message.warning('日期跨度至多两年。')
              }
              return !(_this.$tool.dateDiff(val.crStartDate, val.crEndDate).Y >= 2)
            }
          })
        }
      },
      {
        field: 'creater',
        title: '创建人',
        minWidth: 180,
        header: {
          filter: {
            title: '创建人',
            key: 'createrId',
            placeholder: '请选择创建人',
            load: (o) => {
              o.select.options = clueCustomerMgr.selectTeamUser('', false)
            },
            select: {
              options: [],
              multiple: false,
              filterable: true,
              remote: (key) => {
                return clueCustomerMgr.selectTeamUser(key, false)
              }
            }
          }
        }
      }
    ]
    let handel = [
      {
        field: 'handles',
        title: '操作项',
        fixed: 'right',
        visible: _this.curState != 'case',
        minWidth: 100,
        slots: {
          default: (params, h) => {
            let btns = []
            btns.push({
              title: _this.curState == 'user' ? '查看详情' : '',
              event: 'checkFun',
              data: params.row
            })

            btns.push({
              title: _this.curState == 'content' ? '删除' : '',
              event: 'delFun',
              data: params.row
            })
            return table.cell.btns(_this, h, btns)
          }
        }
      }
    ]
    if (_this.curState == 'case') {
      this.all = [...company, ...caseList]
    } else if (_this.curState == 'user') {
      this.all = [...company, ...userList, ...handel]
    } else if (_this.curState == 'content') {
      this.all = [...company, ...contentList, ...handel]
    } else {
      this.all = [...company]
    }
  }
}

export default Column
